import axios from 'axios'
import storage from './storage'
import router from '../router/index'
import {
  catchError
} from "./catchError";


const http = axios.create({
  //baseURL: process.env.VUE_APP_BASE_API,
  timeout: 10000
});
http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
http.defaults.headers['Access-Control-Allow-Origin'] = '*';
//http.defaults.headers['contentType'] = 'application/json;charset=utf-8';
//错误处理

http.interceptors.request.use(config => {
  let token = storage.sessionGet('token');
  let name = storage.sessionGet('name');
  if (token) {
    config.headers.Authorization = token;
  }
  return config
}, error => {
  //console.log(error)
  //catchError(error)
});

http.interceptors.response.use(response => {
  window.isRefreshing = false;
  const that = this;
  if (!window.isRefreshing) {
    if (response.data.errcode == '30001') {

      // router.replace({
      //   path: '/login',
      //   query: {
      //     redirect: router.currentRoute.fullPath
      //   } //登录成功后跳入浏览的当前页面
      // })
    }
    window.isRefreshing = true;
  }
  return response
}, error => {
  catchError(error)
  //console.log(error)
});
export default http;